package com.wx.wxpush.controller;

//import com.wx.wxpush.job.SpringDynamicCornTask;
import com.wx.wxpush.job.SpringDynamicCornTaskNight;
import com.wx.wxpush.mapper.CornTaskMapper;
import com.wx.wxpush.util.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@Controller
@RequestMapping("/job")
@Api(tags="定时器设置接口")
public class JobController {

//    @Autowired
//    private SpringDynamicCornTask springDynamicCornTask;
    @Autowired
    private SpringDynamicCornTaskNight springDynamicCornTaskNight;
    @Autowired
    private CornTaskMapper cornTaskMapper;

    @ApiOperation(value = "设置自定义定时器", notes = "入参（* * * * * ?），参考：http://cron.ciding.cc/")
    @PutMapping("/setCron")
    @ResponseBody
    public ResultVO setCron(@RequestParam("cronMoring")String cronMoring,
                            @RequestParam("openid") String openid,
                            @RequestParam("cronNight")String cronNight){
        //判断是否存在 存在即更新不插入
        String s = cornTaskMapper.selectByOpenid(openid);
        System.out.println(s+"sss");
        if(s == null){
            int i= cornTaskMapper.insertCron(openid,cronMoring,cronNight);
        }else {
           int i= cornTaskMapper.updateByOpenid(openid,cronMoring,cronNight);
        }

//        //int i= cornTaskMapper.insertCron(openid,cronMoring,cronNight);
//        //设置早晚定时
//        List<String> list=new ArrayList<>();
//        list = cornTaskMapper.selectList();
//        for (String morcons:list){
//            System.out.println(morcons+"iiiiiiii");
//           // springDynamicCornTask.setCron(cronMoring);
//        }
//       //
//        springDynamicCornTaskNight.setCron(cronNight);

        return ResultVO.ok();
    }
//    @GetMapping("/getCron")
//    @ApiOperation(value = "获取当前定时规则", notes = "参考：http://cron.ciding.cc/")
//    public String getCron(){
//      //  String cron = springDynamicCornTask.getCron();
//        return "cron:"+cron;
//    }
}

